package h0907;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
  * @description 全排列
  * @author 不知名帅哥
  * @date 2024/9/7 21:27
  * @version 1.0
*/
public class AllPermutations {
    public static void main(String[] args) {

    }
    public List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> res=new ArrayList<>();
        LinkedList<Integer> temp = new LinkedList<>();
        boolean[] used=new boolean[nums.length];
        backTrack(nums,used,res,temp);
        return res;
    }

    private void backTrack(int[] nums, boolean[] used, List<List<Integer>> res, LinkedList<Integer> temp) {
        if (temp.size()== nums.length){
            res.add(new LinkedList<>(temp));
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            if (!used[i]){
                temp.add(nums[i]);
                used[i]=true;
                backTrack(nums, used, res, temp);
                used[i]=false;
                temp.removeLast();
            }
        }
    }
}
